package com.miyu.system.controller.sys;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.miyu.common.utils.commonUtils.RedisCache;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.stream.Collectors;

/**
 * 系统接口访问
 * 需要登录才能访问此类下接口
 * @author longmy
 */
@RestController
@RequestMapping("/system/log")
@SaCheckLogin
public class SysController {
    @SaCheckLogin
    @RequestMapping("isCheck")
    public JSONObject isCheck(@RequestBody JSONObject param){
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        System.out.println(tokenInfo);
        JSONObject rs = new JSONObject();
        return rs;
    }
    /**
     * api调用日志
     * @param param
     * @return
     */
    @RequestMapping("queryApiLog")
    public JSONArray queryApiLog(@RequestBody JSONObject param){
        JSONArray cosApiLog = JSONArray.of(RedisCache.getJSONArray(1, "cosApiLog"));
        long beginDate = param.getLong("beginDate");
        long endDate = param.getLong("endDate");
        JSONArray rs = cosApiLog.stream()
                .filter(item -> DateUtil.parse(((JSONObject)item).getString("dateApi")).isIn(new DateTime(DateUtil.date(beginDate)),new DateTime(DateUtil.date(endDate))))
                .collect(Collectors.toCollection(JSONArray::new));
        return  rs;
    }
    /**
     * 登录日志查询
     * @param param
     * @return
     */
    @RequestMapping("queryLoginLog")
    public JSONArray queryLoginLog(@RequestBody JSONObject param){
        long beginDate = param.getLong("beginDate");
        long endDate = param.getLong("endDate");
        JSONArray cosApiLog = JSONArray.of(RedisCache.getJSONArray(1, "cosLoginLog"));
        JSONArray rs = cosApiLog.stream()
                .filter(item -> DateUtil.parse(((JSONObject)item).getString("logDate")).isIn(new DateTime(DateUtil.date(beginDate)),new DateTime(DateUtil.date(endDate))))
                .collect(Collectors.toCollection(JSONArray::new));
        return  rs;
    }


}
